﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Director.DomainModels;

namespace Director
{
	class DataManager
	{
		readonly string[] _shareCodes;

		private static readonly DataManager _instance = new DataManager();

		private DataManager()
		{
			DirectorStorage = new DirectorStorage();

			// акции, интересные для работы
			_shareCodes = @"
				AFLT.MM,BANE.MM,CHMF.MM,FEES.MM,
				GAZP.MM,HYDR.MM,LKOH.MM,
				MAGN.MM,MTLR.MM,MTSS.MM,NLMK.MM,
				NVTK.MM,OGKB.MM,RASP.MM,ROSN.MM,
				RSTI.MM,RTKM.MM,SBER.MM,SIBN.MM,
				SNGSP.MM,TATN.MM,URKA.MM,VTBR.MM,PLZL.MM".Split(new[] { ',', '\r', '\n', '\t', ' ' },StringSplitOptions.RemoveEmptyEntries);
		}

		public static DataManager Instance { get { return _instance; } }
	
		public DirectorStorage DirectorStorage { get; private set; }

		public Board[] Boards { get; set; }
		public TimeFrame[] TimeFrames { get; set; }
		public Dictionary<int, Security> Securities { get; set; }
		public Market[] Markets { get; set; }
		public Position[] Positions { get; set; }
		
		public IEnumerable<Security> SharesByCodes
		{
			get
			{
#if DEBUG
				var t = Securities.Values.Where(e => _shareCodes.Contains(e.Code) && e.Lotsize == 0).ToArray();
				if (t.Length > 0)
				{
					foreach (var security in t)
					{
						Debug.WriteLine("*********** " + security.Code + " " + security.Name);
					}
					if (Debugger.IsAttached)Debugger.Break();
				}
#endif
				return Securities.Values.Where(e => _shareCodes.Contains(e.Code));
			}
		}

		public TimeFrame this[TimeFrame.ELength length]
		{
			get { return TimeFrames.Single(e => e.TimeFrameELength == length); }
		}
	}
}
